from flask_restful import marshal, reqparse
from flask_restful import marshal_with, abort
from flask_restful import fields, Resource
from werkzeug.datastructures import FileStorage

from apps.apis.admin.utils import login_required
from apps.apis.common.utils import filename_transfer
from apps.models.common import Movie

parse = reqparse.RequestParser()

parse.add_argument("show_name", required=True, help="请提供电影的中文名")
parse.add_argument("show_name_en", required=True, help="请提供电影的英文名")
parse.add_argument("director", required=True, help="请提供电影的导演")
parse.add_argument("main_acts", required=True, help="请提供电影的主演")
parse.add_argument("type", required=True, help="请提供电影的类型")
parse.add_argument("country", required=True, help="请提供电影是哪国的")
parse.add_argument("language", required=True, help="请提供电影的语言")
parse.add_argument("duration", required=True, help="请提供电影的时长")
parse.add_argument("projection", required=True, help="请提供电影的放映技术")
parse.add_argument("release", required=True, help="请提供电影的上线时间")
parse.add_argument("bgp", type=FileStorage, required=True, help="请提供电影的背景图片", location=['files'])

movie_fields = {
    "show_name": fields.String,
    "show_name_en": fields.String,
    "director": fields.String,
    "main_acts": fields.String,
    "type": fields.String,
    "country": fields.String,
    "language": fields.String,
    "duration": fields.Integer,
    "projection": fields.String,
    "release": fields.DateTime,
    "bgp": fields.String,
}

multi_movies_fields = {
    "status": fields.Integer,
    "msg": fields.String,
    "data": fields.List(fields.Nested(movie_fields))
}


class MoviesResource(Resource):
    @marshal_with(multi_movies_fields)
    def get(self):

        movies = Movie.query.all()

        data = {
            "msg": "ok",
            "status": 200,
            "data": movies
        }

        return data

    @login_required
    def post(self):
        args = parse.parse_args()

        show_name = args.get("show_name")
        show_name_en = args.get("show_name_en")
        director = args.get("director")
        main_acts = args.get("leadingRole")
        movie_type = args.get("type")
        country = args.get("country")
        language = args.get("language")
        duration = args.get("duration")
        projection = args.get("projection")
        release = args.get("release")
        bgp = args.get("bgp")

        # bgp = request.files.get("bgp")

        movie = Movie()
        movie.show_name = show_name
        movie.show_name_en = show_name_en
        movie.director = director
        movie.leadingRole = main_acts
        movie.type = movie_type
        movie.country = country
        movie.language = language
        movie.duration = duration
        movie.projection = projection
        movie.release = release

        print(type(bgp))

        print(bgp)

        file_info = filename_transfer(bgp.filename)

        bgp_dir = file_info[1]

        bgp.save(bgp_dir)

        movie.bgp = file_info[0]

        if not movie.save():
            abort(400, msg="can't create movie")

        data = {
            "msg": "create success",
            "status": 201,
            "data": marshal(movie, movie_fields)
        }

        return data


class MovieResource(Resource):
    def get(self, id):
        movie = Movie.query.get(id)
        if not movie:
            abort(404, msg="movie is not exist")
        data = {
            "msg": "ok",
            "status": 200,
            "data": marshal(movie, movie_fields)
        }
        return data

    @login_required
    def patch(self, id):
        return {'msg': "post ok"}

    @login_required
    def put(self, id):
        return {'msg': "post ok"}

    @login_required
    def delete(self, id):
        return {'msg': "post ok"}

